package com.dayu.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayu.lease.common.exception.LeaseException;
import com.dayu.lease.common.result.ResultCodeEnum;
import com.dayu.lease.model.entity.*;
import com.dayu.lease.model.enums.ItemType;
import com.dayu.lease.web.admin.mapper.*;
import com.dayu.lease.web.admin.service.*;
import com.dayu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.dayu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.dayu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.dayu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.dayu.lease.web.admin.vo.fee.FeeValueVo;
import com.dayu.lease.web.admin.vo.graph.GraphVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Resource
    private  ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private LabelInfoMapper labelInfoMapper;
    @Resource
    private FacilityInfoMapper facilityInfoMapper;
    @Resource
    private FeeValueMapper feeValueMapper;
    @Resource
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);
        if (isUpdate) {
            LambdaQueryWrapper<GraphInfo> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            wrapper1.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(wrapper1);
            LambdaQueryWrapper<ApartmentFacility> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(wrapper2);
            LambdaQueryWrapper<ApartmentLabel> wrapper3 = new LambdaQueryWrapper<>();
            wrapper3.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(wrapper3);
            LambdaQueryWrapper<ApartmentFeeValue> wrapper4 = new LambdaQueryWrapper<>();
            wrapper4.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(wrapper4);
        }
        //新增
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = graphVoList.stream().map(u -> {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(u.getName());
                graphInfo.setUrl(u.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                return graphInfo;
            }).collect(toList());
            graphInfoService.saveBatch(graphInfoList);
        }
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<ApartmentFacility> list = facilityInfoIds.stream().map(u -> {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .facilityId(u)
                        .build();
                return apartmentFacility;
            }).collect(toList());
            apartmentFacilityService.saveBatch(list);
        }
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            List<ApartmentFeeValue> list = feeValueIds.stream().map(u -> {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .feeValueId(u)
                        .build();
                return apartmentFeeValue;
            }).collect(toList());
            apartmentFeeValueService.saveBatch(list);
        }
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            List<ApartmentLabel> list = labelIds.stream().map(u -> {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .labelId(u)
                        .build();
                return apartmentLabel;
            }).collect(toList());
            apartmentLabelService.saveBatch(list);
        }
    }

    @Override
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo vo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo, vo);
        List<GraphVo> list=graphInfoMapper.selectListByIdAndType(ItemType.APARTMENT,id);
        vo.setGraphVoList(list);
        List<LabelInfo> labelInfoList=labelInfoMapper.selectListByApartmentId(id);
        vo.setLabelInfoList(labelInfoList);
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.selectListByApartmentId(id);
        vo.setFacilityInfoList(facilityInfoList);
        List<FeeValueVo> feeValueVoList=feeValueMapper.selectListByApartmentId(id);
        vo.setFeeValueVoList(feeValueVoList);
        return vo;
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoomInfo::getApartmentId,id);
        Long count = roomInfoMapper.selectCount(wrapper);
        if (count>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        super.removeById(id);
        LambdaQueryWrapper<GraphInfo> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(GraphInfo::getItemId, id);
        wrapper1.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(wrapper1);
        LambdaQueryWrapper<ApartmentFacility> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(wrapper2);
        LambdaQueryWrapper<ApartmentLabel> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(wrapper3);
        LambdaQueryWrapper<ApartmentFeeValue> wrapper4 = new LambdaQueryWrapper<>();
        wrapper4.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(wrapper4);
    }
}




